package com.hengxing.controller;

import com.alibaba.druid.support.json.JSONParser;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.hengxing.domain.Book;
import com.hengxing.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
@ResponseBody
public class BookController {

  @Autowired
    private BookService bookService;


    @PostMapping
    public Result save(@RequestBody Book book) {
        Boolean bool = bookService.save(book);
        return new Result(bool?Code.SAVE_OK:Code.SAVE_ERR,bool,bool?Code.SAVE_OK_MSG:Code.SAVE_ERR_MSG);
    }

    @PutMapping
    public Result updata(@RequestBody Book book) {
        Boolean bool = bookService.updata(book);
        return new Result(bool?Code.UPDATA_OK:Code.UPDATA_ERR,bool, bool?Code.UPDATA_OK_MSG:Code.UPDATA_ERR_MSG);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        Boolean bool = bookService.delete(id);
        return new Result( bool?Code.DELETE_OK:Code.DELETE_ERR,bool,bool?Code.DELETE_OK_MSG:Code.DELETE_ERR_MSG);

    }

   @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
      return new Result( book != null ?Code.GET_OK:Code.GET_ERR,book, book != null ?Code.GET_OK_MSG:Code.GET_ERR_MSG);
    }

    @GetMapping
    public Result getAll() {

        List<Book> books = bookService.getAll();
        return new Result(books != null ?Code.GET_OK:Code.GET_ERR,books, books != null ?Code.GET_OK_MSG:Code.GET_ERR_MSG);
    }
}
